
#include "musicplayer.h"
using namespace std;

MusicPlayer::MusicPlayer():sems(1)
{
    isPlaying = false;
    isLooping = false;
    isShuffling = false;
    currentMusic = 0;
    totalMusic = 0;
    int pid = -1;
    sems.init_sem(sems.get_semid(),0,1);
}

void MusicPlayer::play()
{   p();
    if (isPlaying == true)
    {
        cout << "歌曲：" << name[currentMusic] << "已经在播放了\n"
             << endl;
    }
    int pid = fork();
    if (pid < 0)
    {
        perror("play fork error!\n");
        exit(1);
    }
    else if (pid == 0)
    {
        string cmd = "./music/" + name[currentMusic];
        execlp("mpg321", "mpg321", cmd.c_str(), NULL);
        perror("execlp 调用失败\n");
        exit(1);
    }
    else if (pid > 0)
    {
        this->pid = pid;
        this->isPlaying = true;
    }
    v();
}

void MusicPlayer::pause()
{   p();
    if (isPlaying && pid > 0)
    {
        if (kill(pid, SIGSTOP) == 0)
        {
            isPlaying = false;
            cout << "暂停中" << endl;
        }
        else
        {
            perror("暂停失败！");
        }
    }
    else
    {
        cout << "没有播放歌曲!" << endl;
    }
    v();
}

void MusicPlayer::next()
{
    p();
    if (isPlaying && pid > 0)
    {
        kill(pid, SIGKILL);
        waitpid(pid, NULL, 0);
        isPlaying = false;
    }
    currentMusic++;
    if (currentMusic >= totalMusic)
        currentMusic = 0;
    play();
    v();
}

void MusicPlayer::loop()
{   p();
    if (isLooping)
    {
        isLooping = false;
        cout << "单曲循环播放关闭" << endl;
    }
    else
    {
        isLooping = true;
        cout << "单曲循环播放开启" << endl;
    }
    v();
}

void MusicPlayer::shuffle()
{   p();
    if (isShuffling)
    {
        isShuffling = false;
        cout << "随机播放关闭" << endl;
    }
    else
    {
        isShuffling = true;
        cout << "随机播放开启" << endl;
    }
    v();
}

void MusicPlayer::addMusic(string name)
{   p();
    if (++totalMusic > 100)
    {
        cout << "音乐库已满！" << endl;
        return;
    }
    this->name[--totalMusic] = name;
    v();
}

void MusicPlayer::removeMusic(string name)
{   p();
    if (--totalMusic < 0)
    {
        cout << "音乐库为空！" << endl;
        return;
    }
    this->name[--totalMusic] = name;
    v();
}

void MusicPlayer::autoPlay()
{   p();
    if (isAutoPlay == true)
    {
        isAutoPlay = false;
        cout << "自动播放已关闭" << endl;
    }
    else
    {
        isAutoPlay = true;
        cout << "自动播放已开启" << endl;
        if (pid <= 0 || !isPlaying)
        {
            play();
        }
        if (pid > 0 && isPlaying)
        {
            while (totalMusic > 0)
            {
                waitpid(pid, NULL, 0);
                if (isLooping == false)
                {
                    currentMusic++;
                    if (currentMusic >= totalMusic)
                        currentMusic = 0;
                }
                if(isShuffling == true)
                {
                    currentMusic = rand() % totalMusic;
                }
                play();
                if (isAutoPlay == false)
                    break;
            }
        }
    }
    v();
}

void MusicPlayer::p()
{
    int semid=sems.get_semid();
    sems.p_sem(semid,0);
}

void MusicPlayer::v()
{
    int semid=sems.get_semid();
    sems.v_sem(semid,0);
}
